package com.cloudkinto.controller.fbaorderoutput;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.fbaorderoutput.FbaOrderOutputService;
import com.cloudkinto.service.fbaorderoutput.vo.*;
import com.cloudkinto.service.file.FileService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-04
 */
@RestController
@Api(tags = "OpenApi-FBA订单出库")
@RequestMapping("/api/open/fbaOrderOutput")
public class FbaOrderOutputApiController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FbaOrderOutputApiController.class);

    @Autowired
    private FbaOrderOutputService service;

    @Autowired
    private FileService fileService;

    @UserSessionRequired
    @PostMapping("add")
    @ApiOperation(value = "新增出库", notes = "新增FBA转运订单出库")
    public SingleResult add(@RequestBody @Validated FbaOrderOutputOpenAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        req.setCompanyId(companyId);
        String value = service.fbaStorageOpenAdd(req, userId);
        if (value != null) {
            return SingleResult.success(value);
        } else {
            return SingleResult.failure("500","添加失败");
        }
    }

    @UserSessionRequired
    @GetMapping("tracklist")
    @ApiOperation(value = "费用预算", notes = "费用预算")
    public SingleResult tracklist(Long storageId, Long fbaStorageId) {
        Long companyId = getCompanyId();
        return service.tracklist(storageId, fbaStorageId, companyId);
    }

    @UserSessionRequired
    @PostMapping(value = "附件上传")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public SingleResult uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) {
        Long userId = null;
        return fileService.uploadTempFile(file, userId);
    }

    @UserSessionRequired
    @PutMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated OpenAPIFbaOrderUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.openApiUpdate(req, getCompanyId(), userId));
    }

//    @UserSessionRequired
//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "根据id删除", notes = "根据id删除")
//    public SingleResult delete(@PathVariable("id") Long id) {
//        Long userId = getCurrentUserId();
//        return SingleResult.success("删除成功", service.openApiDelete(id, userId));
//    }

    @UserSessionRequired
    @DeleteMapping("/{orderNumber}")
    @ApiOperation(value = "根据orderNumber删除", notes = "根据orderNumber删除")
    public SingleResult delete(@PathVariable("orderNumber") String orderNumber) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.openApiDelete(orderNumber, userId));
    }

//    @UserSessionRequired
//    @GetMapping("/{id}")
//    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
//    @ApiResponses({@ApiResponse(code = 200, message = "", response = FbaOrderOutputDetailRes.class)})
//    public SingleResult<FbaOrderOutputDetailRes> detail(@PathVariable("id") Long id) {
//        return SingleResult.success("查询成功", service.apiDetail(id));
//    }


  @UserSessionRequired
    @GetMapping("/detail")
    @ApiOperation(value = "根据orderNumber查看详情", notes = "根据orderNumber查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = FbaOrderOutputDetailRes.class)})
    public SingleResult<FbaOrderOutputDetailRes> detail(String orderNumber) {
        return SingleResult.success("查询成功", service.apiDetail(orderNumber));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字搜索"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "status", value = "订单状态"),
            @ApiImplicitParam(name = "trackId", value = "快递公司"),
            @ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = FbaOrderOutputListRes.class)})
    public PageResult<FbaOrderOutputListRes> listPage(String keyword, String current, String pageSize,
                                                      String status, String startDate, String endDate,
                                                      String trackId, String shopId, String storageId) {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("status", status);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("trackId", trackId);
        map.put("storageId", storageId);
//        map.put("orderType", "2");//fba转运
        if (!StringUtils.isEmpty(shopId)) {
            map.put("shopId", shopId);
        } else if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("companyId", companyId + "");
        return service.getListPage(map);
    }

}

